package com.example.presentshopping.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.IntentUtils;
import com.example.presentshopping.R;
import com.example.presentshopping.config.bean.DetailsBean;
import com.example.presentshopping.ui.activity.DetailsActivity;
import com.example.presentshopping.ui.activity.OrdermlyActivity;
import com.example.presentshopping.ui.custom.NoDoubleClickListener;
import com.example.presentshopping.ui.custom.RoundImageView;
import com.example.presentshopping.ui.custom.SelectedButton;
import com.example.presentshopping.utils.glide.GlideUtil;
import com.example.presentshopping.utils.tool.IntentJumpUtils;
import com.example.presentshopping.utils.tool.JsonHelper;

public class SukDialog {

    private DetailsActivity mContext;
    private Dialog mDialog;
    private DetailsBean detailsBean;
    private String Miassactiviy;
    private Display mDisplay;

    // 获取控件高度
    private int mHeight = 0;
    private int team_type = 1;
    // 1拼团 2非拼团
    private int is_team = 1;
    private int number = 1;

    private RoundImageView dialog_spec_select_pic;
    private ImageView dialog_spec_select_close;
    private RelativeLayout romes_sie;
    private LinearLayout lytoo_sc;

    private SelectedButton dialog_spec_select_reduce, dialog_spec_select_add;
    private TextView telename, marketprice, total,dialog_spec_select_num;
    private Button oncebu, unlimitedbu;

    public SukDialog(DetailsActivity context, DetailsBean detailsBean,String Miassactiviy) {
        this.mContext = context;
        this.detailsBean = detailsBean;
        this.Miassactiviy =Miassactiviy;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        this.mDisplay = windowManager.getDefaultDisplay();
    }

    public SukDialog builder() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_sku_select, null);
        view.setMinimumWidth(mDisplay.getWidth());
        initView(view);

        // 定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.CommonDialogStyle);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.BottomDialogAnimation);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (mDisplay.getWidth());
        lp.height = (int) (mDisplay.getHeight() * 0.55);
        //   lp.height = romes_sie.getHeight();
        Log.e("SukDialog", "" + lp.height);
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    public void initView(View view) {
        dialog_spec_select_pic = view.findViewById(R.id.dialog_spec_select_pic);
        dialog_spec_select_close = view.findViewById(R.id.dialog_spec_select_close);

        dialog_spec_select_num = view.findViewById(R.id.dialog_spec_select_num);


        romes_sie = view.findViewById(R.id.romes_sie);
        lytoo_sc = view.findViewById(R.id.lytoo_sc);

        dialog_spec_select_reduce = view.findViewById(R.id.dialog_spec_select_reduce);
        dialog_spec_select_add = view.findViewById(R.id.dialog_spec_select_add);

        telename = view.findViewById(R.id.telename);
        marketprice = view.findViewById(R.id.marketprice);
        total = view.findViewById(R.id.total);

        oncebu = view.findViewById(R.id.oncebu);
        unlimitedbu = view.findViewById(R.id.unlimitedbu);

        SelectedButton dialog_spec_select_ok = view.findViewById(R.id.dialog_spec_select_ok);


        dialog_spec_select_close.setOnClickListener(onClickListener);
        dialog_spec_select_ok.setOnClickListener(onClickListener);
        oncebu.setOnClickListener(onClickList);
        unlimitedbu.setOnClickListener(onClickList);

        dialog_spec_select_reduce.setOnClickListener(onClickList);
        dialog_spec_select_add.setOnClickListener(onClickList);

        GlideUtil.load(mContext, detailsBean.getData().getThumb(), dialog_spec_select_pic);

        if (detailsBean.getData().getIsteam() == 1) {
          if (Miassactiviy.contains("ProductListFragment")){
              lytoo_sc.setVisibility(View.GONE);
          }else {
              lytoo_sc.setVisibility(View.VISIBLE);
              dialog_spec_select_reduce.setVisibility(View.GONE);
              dialog_spec_select_add.setVisibility(View.GONE);
          }
        } else {
            lytoo_sc.setVisibility(View.GONE);
        }

        telename.setText(detailsBean.getData().getTitle());
        marketprice.setText("¥" + ":" + detailsBean.getData().getMarketprice());
        total.setText("库存：" + detailsBean.getData().getTotal());
        romes_sie.setMinimumHeight(100);

    }

    public View.OnClickListener onClickListener = new NoDoubleClickListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        protected void onNoDoubleClick(View v) throws Exception {
            switch (v.getId()) {
                case R.id.dialog_spec_select_close:
                    dismiss();
                    break;
                case R.id.dialog_spec_select_ok:
                    if (IntentJumpUtils.isLogin()) {
                    String JsonHer =  JsonHelper.newtoJson(detailsBean);
                    Intent intent = new Intent(mContext, OrdermlyActivity.class);
                    intent.putExtra("team_type", ""+team_type);
                    intent.putExtra("goods_id",""+ detailsBean.getData().getId());
                    if (Miassactiviy.contains("ProductListFragment")){
                        intent.putExtra("is_team",""+ 2);
                        lytoo_sc.setVisibility(View.GONE);
                    }else {
                        intent.putExtra("is_team",""+ detailsBean.getData().getIsteam());
                    }
                    intent.putExtra("activity",Miassactiviy);
                    intent.putExtra("number",""+ number);
                    intent.putExtra("detailsBean",JsonHer);
                    mContext.startActivity(intent);
                    mContext.finish();
                    dismiss();
                    } else {
                        // 登录
                        IntentJumpUtils.goToLogin(mContext, "");
                    }
                    break;

            }
        }
    };

    public View.OnClickListener onClickList = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.oncebu:
                    team_type = 1;
                    Log.e("OrdermlyActivity", "--oncebu-" + team_type);
                    oncebu.setBackgroundResource(R.drawable.shape_maincolor_line_14);
                    unlimitedbu.setBackgroundResource(R.drawable.shape_maincolor_line_13);

                    break;
                case R.id.unlimitedbu:
                    team_type = 2;
                    unlimitedbu.setBackgroundResource(R.drawable.shape_maincolor_line_14);
                    oncebu.setBackgroundResource(R.drawable.shape_maincolor_line_13);
                    Log.e("OrdermlyActivity", "--unlimitedbu-" + team_type);
                    break;
                case  R.id.dialog_spec_select_reduce:
                    if (number>1){
                        number =number - 1 ;

                        dialog_spec_select_num.setText(""+number);
                    }

                    break;
                case R.id.dialog_spec_select_add:

                        number =number + 1 ;
                        dialog_spec_select_num.setText(""+number);

                    break;

            }
        }
    };

    public SukDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    public void show() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    private onSelectListener mListener;

    public void setOnSelectListener(onSelectListener listener) {
        this.mListener = listener;
    }


    public interface onSelectListener {
        /**
         * 规格选择完毕回调
         *
         * @param goodId    产品ID
         * @param count     产品数量
         * @param sku       产品规格，逗号隔开
         * @param goodPrice 产品价格
         */
        void onComplete(long goodId, int count, String sku, String goodPrice);
    }


}
